using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using FacebookWrapper.ObjectModel;

namespace B12_Ex03_Eran_053094363_Assaf_036722999
{
    public partial class MainForm : Form
    {
        private const string k_EventsLinkText = "Events:";
        private const string k_FriendsLinkText = "Friends:";
        private const string k_NewsFeedLinkText = "News feed:";
        private const string k_CheckInsLinkText = "Check ins:";
        private const string k_MyAppAccesssToken = "322549387800673"; // New Test App ID "416037895076426"
        
        private bool v_LoggedIn;
        private FacebookManager m_FacebookManager;

        public MainForm()
        {
            InitializeComponent();

            // Initialize the Private Properties
            m_FacebookManager = FacebookManager.Instance;
            this.autoLogin();
        }

        private void autoLogin()
        {
            if (m_FacebookManager.HasAutoLogin())
            {
                initLogin();
                loggedIn();
            }
            else
            {
                loggedOut();
            }
        }

        private void loggedIn()
        {
            v_LoggedIn = true;
            this.btnLogIn.Enabled = false;
            this.btnLogout.Enabled = true;
            pictureProfile.Show();
        }

        private void loggedOut()
        {
            v_LoggedIn = false;
            this.btnLogIn.Enabled = true;
            this.btnLogout.Enabled = false;
            pictureProfile.Hide();
            this.listBoxCheckIns.Items.Clear();
            this.listBoxEvents.Items.Clear();
            this.listBoxFriends.Items.Clear();
            this.listBoxNews.Items.Clear();
            this.linkCheckIns.Text = k_CheckInsLinkText;
            this.linkFriendsList.Text = k_FriendsLinkText;
            this.linkNewsFeed.Text = k_NewsFeedLinkText;
            this.linkEvents.Text = k_EventsLinkText;
        }

        /// <summary>
        /// Fill the Friends List Box
        /// with the Users Friends
        /// </summary>
        private void getFriendsList()
        {
            var friendsCollection = m_FacebookManager.GetFriends();
            listBoxFriends.DisplayMember = "Name";
            listBoxFriends.DataSource = friendsCollection;

            // Update the Label Text
            linkFriendsList.Text = string.Format("{0} ({1})", k_FriendsLinkText, friendsCollection.Count);
        }

        /// <summary>
        /// Fill the Events List Box
        /// </summary>
        private void getEventsList()
        {
            var eventsCollection = m_FacebookManager.GetEvents();
            listBoxEvents.DisplayMember = "Description";
            listBoxEvents.DataSource = eventsCollection;

            // Update the Label Text
            linkEvents.Text = string.Format("{0} ({1})", k_EventsLinkText, eventsCollection.Count);
        }

        /// <summary>
        /// Fill the News feed List Box
        /// </summary>
        private void getNewsList()
        {
            var newsCollection = m_FacebookManager.GetNewsFeed();
            listBoxNews.Items.Clear();
            
            // Iterate the NewsFeed
            foreach (var item in newsCollection)
            {
                listBoxNews.Items.Add(item);
            }

            // Update the Label Text
            linkNewsFeed.Text = string.Format("{0} ({1})", k_NewsFeedLinkText, newsCollection.Count);
        }

        /// <summary>
        /// Fill the Check in List Box
        /// with User Check ins'
        /// </summary>
        private void getCheckInsList()
        {
            var checkinCollection = m_FacebookManager.GetCheckIns();
            listBoxCheckIns.Items.Clear();
            foreach (var item in checkinCollection)
            {
                listBoxCheckIns.Items.Add(string.Format("{0} Checked in at {1} on {2}", item.From.Name, item.Place.Location, item.CreatedTime));
            }

            // Update the Label Text
            linkCheckIns.Text = string.Format("{0} ({1})", k_CheckInsLinkText, checkinCollection.Count);
        }

        /// <summary>
        /// stream_post a New Status
        /// </summary>
        private void postStatus()
        {
            m_FacebookManager.PostNewMessageStatus(txtUpdateStatus.Text);
        }

        /// <summary>
        /// Open the Freind Filter Form
        /// </summary>
        private void listBoxFriends_SelectedIndexChanged(object sender, EventArgs e)
        {
            displaySelectedFriend();
        }

        private void displaySelectedFriend()
        {
            if (listBoxFriends.SelectedItems.Count == 1)
            {
                User selectedFriend = listBoxFriends.SelectedItem as User;
                if (selectedFriend.PictureNormalURL != null)
                {
                    pictureFriend.LoadAsync(selectedFriend.PictureNormalURL);
                }
                else
                {
                    pictureFriend.Image = pictureFriend.ErrorImage;
                }
            }
        }

        /// <summary>
        /// Open the Friend Filter Form
        /// </summary>
        private void filterFriends()
        {
            // Create a new Friends Form
            FriendsForm friendsForm = new FriendsForm();

            // Hide the Main Form and show the Friends Form
            this.Hide();

            // Show the Form
            friendsForm.ShowDialog();

            // When Friends Form Closes - Show the Main form again
            this.Show();
        }

        private void initLogin()
        {
            try
            {
                if (m_FacebookManager.Login(k_MyAppAccesssToken))
                {
                    pictureProfile.LoadAsync(m_FacebookManager.GetProfilePicture());
                    loggedIn();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnLogIn_Click(object sender, EventArgs e)
        {
            initLogin();
        }

        private void btnUpdateStatus_Click(object sender, EventArgs e)
        {
            postStatus();
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            filterFriends();
        }

        private void btnLogout_Click(object sender, EventArgs e)
        {
            m_FacebookManager.Logout();
            loggedOut();
        }

        private void listBoxFriends_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listBoxFriends.SelectedItem != null)
            {
                FriendDetails frmDetails = new FriendDetails();
                frmDetails.ShowDetails((User)listBoxFriends.SelectedItem);
            }
        }

        private void linkFriendsList_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (v_LoggedIn)
            {
                getFriendsList();
            }
        }

        private void linkNewsFeed_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (v_LoggedIn)
            {
                getNewsList();
            }
        }

        private void linkEvents_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (v_LoggedIn)
            {
                getEventsList();
            }
        }

        private void linkCheckIns_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (v_LoggedIn)
            {
                getCheckInsList();
            }
        }
    }
}